package com.younion.action.consumer.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IRegisterUserService;
import com.younion.business.ISendMsgService;
import com.younion.common.constants.MsgStatus;
import com.younion.model.SeekPsdResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.MD5;
import com.younion.vo.CustomerVO;
import com.younion.vo.RegisterUserVO;
import com.younion.vo.SendMsgVO;

public class SeekPsdConsumer extends AbstractRequestConsumer {
	
	private IRegisterUserService registerUserService = null;
	
	private ISendMsgService sendMsgService = null;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return seekPsd(request, response);
	}

	public String seekPsd(HttpServletRequest request,
			HttpServletResponse response) {
		SeekPsdResult seekPsdResult = new SeekPsdResult();
		String loginId = request.getParameter("login_id");
		Map paramMap = new HashMap();
		ResultObject resultObject = new ResultObject();
		try {
			RegisterUserVO registerUserVO = registerUserService
					.getVOByMap(paramMap);
			if (registerUserVO == null) {//不存在此用户
				resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
				seekPsdResult.setSuccessful("0");
				resultObject.setResult(seekPsdResult);
				resultObject.setMsg("不存在此用户");
			} else {// 随机生成4位数进行密码重置并发送短信通知
				String newPassword =Long.toString(Math.round(Math.random()*9000+1000));
				registerUserVO.setPassword(MD5.getMD5(newPassword.getBytes("utf-8")));
				registerUserService.save(registerUserVO);
				changePasswordNotification(newPassword, registerUserVO);
				seekPsdResult.setSuccessful("1");
				resultObject.setResult(seekPsdResult);
				resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
			}
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			seekPsdResult.setSuccessful("0");
			resultObject.setResult(seekPsdResult);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	private void changePasswordNotification(String newPassword, RegisterUserVO registerUserVO) throws Exception {
		String content = "尊敬的用户:您的优联账户密码已经重置为："+newPassword+",请登录后尽快修改！";
		SendMsgVO sms = new SendMsgVO();
		sms.setSendTime(new Date());
		sms.setStatus(MsgStatus.UNSEND.getName());
		sms.setContent(content);
		sms.setTitle("重置密码通知");
		sms.setUsers(registerUserVO.getLoginId());
		sendMsgService.save(sms);
	}

	public void setRegisterUserService(IRegisterUserService registerUserService) {
		this.registerUserService = registerUserService;
	}

	public ISendMsgService getSendMsgService() {
		return sendMsgService;
	}

	public void setSendMsgService(ISendMsgService sendMsgService) {
		this.sendMsgService = sendMsgService;
	}
	
	
}
